package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.atguigu.serviceBash.GlobalException;
import com.atguigu.vod.config.InitVod;
import com.atguigu.vod.config.VoConfig;
import com.atguigu.vod.service.VodService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Override
    public String uploadVideo(MultipartFile multipartFile) throws IOException {

        String filename = multipartFile.getOriginalFilename();
        String title = filename.substring(0, filename.lastIndexOf('.'));

        UploadStreamRequest request = new UploadStreamRequest(VoConfig.accessKey, VoConfig.accessKeyV, title, filename, multipartFile.getInputStream());

        UploadVideoImpl uploader = new UploadVideoImpl();

        UploadStreamResponse response = uploader.uploadStream(request);
//        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        log.info("文件上传中...");
        if (response.isSuccess()) {
            log.info("文件已上传");
            return response.getVideoId();
//            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
            log.info("文件上传失败");
            /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
//            System.out.print("VideoId=" + response.getVideoId() + "\n");
//            System.out.print("ErrorCode=" + response.getCode() + "\n");
//            System.out.print("ErrorMessage=" + response.getMessage() + "\n");

            return null;
        }
    }

    /**
     * id = videoId
     *
     * @param id 删除阿里云视频
     * @return
     */
    @Override
    public boolean removeAlyVideo(String id) {
        try {
//           静态方法初始化
            DefaultAcsClient client = InitVod.initVodClient(VoConfig.accessKey, VoConfig.accessKeyV);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GlobalException("删除失败!", 20001);
        }
        return true;
    }

    //    删除多个视频
    @Override
    public boolean removeListAlyVideo(List<String> ids) {
        try {
            DefaultAcsClient client = InitVod.initVodClient(VoConfig.accessKey, VoConfig.accessKeyV);
            DeleteVideoRequest request = new DeleteVideoRequest();
//            批量删除ids 不为空 且ids长度大于0
            String join = StringUtils.join(ids.toArray(), ",");
            request.setVideoIds(join);
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 获取播放凭证
     *
     * @param videoSourceId videoSourceId
     * @return String
     */
    @Override
    public String getVideoPalyOauth(String videoSourceId) {
        try {
            DefaultAcsClient defaultAcsClient = InitVod.initVodClient(VoConfig.accessKey, VoConfig.accessKeyV);

            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoSourceId);
            request.setAuthInfoTimeout(3000L);
            GetVideoPlayAuthResponse response = defaultAcsClient.getAcsResponse(request);

            String playAuth = response.getPlayAuth();
            log.info(response.getRequestId());
            log.info(playAuth);
            return playAuth;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


}
